import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import Tender
from tools import common_assert, html_parser, read_json


class TestTender:
    def setup_method(self):
        self.session = requests.session()
        self.api = Tender(self.session)
        ApiRegisterLogin(self.session).api_login()

    def teardown_method(self):
        self.session.close()

    @pytest.mark.parametrize("amount,expect_text", read_json("tender.json", "tender"))
    def test01_tender(self, amount, expect_text):
        try:
            # 后台投资接口
            result = self.api.api_tender(amount)
            # print(result.json())
            # print("后台投资结果为：{}".format(result.json()))
            log.info("后台投资接口执行结果为:{}".format(result.json()))

            if expect_text == "OK":
                # 三方投资
                data = html_parser(result)
                result = self.session.post(url=data[0], data=data[1])
                log.info("三方投资执行结果为:{}".format(result.text))
            common_assert(result, expect_text=expect_text)
        except Exception as e:
            log.error(e)
            raise
